#! /usr/bin/python3
# codings=utf-8
# @time: 2025/10/14 19:01
# @Author:鹤川
import json
import os

import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

from common.doLog import DoLog

# r反转义
myPath = r'E:\Work\codings\python\2506project\AndroidTest01\datas'
dl = DoLog()
myLogger = dl.getLogger(__name__)
class DoFile:
    # 读取Excel文件
    def doExcelRead(self, fname):
        fileName = os.path.join(myPath, fname)
        try:
            myLogger.info(f"开始处理文件{fname}")
            wk:Workbook = openpyxl.load_workbook(fileName)   # 获取工作簿的对象
            st:Worksheet = wk['login']   # 获取当前活跃的工作表
            print(st,type(st))
            ss=st.iter_rows(values_only=True)
            print(ss)
        except Exception as e:
            print(f"读取Excel文件{fname}失败，原因：{e}")
            myLogger.exception(f"读取Excel文件{fname}失败，原因：{e}")
        else:
            myLogger.info(f"成功读取Excel文件{fname}")
            l=[i for i in ss]
            print(f"{l}")
            return l
        finally:
            print(f"处理{fname}文件时未发生异常")
            myLogger.info(f"处理{fname}文件时未发生异常")
            wk.close()  # 关闭工作簿对象

    # 自定义打印Excel内容
    def doExcelReadDIV(self, fname, stname, minRow=None, maxRow=None, minCol=None, maxCol=None):
        # 读取指定Excel文件中的数据，并返回指定范围内的单元格值列表
        fileName = os.path.join(myPath, fname)
        try:
            # myLogger.info(f"开始处理文件{fname}")
            wk: Workbook = openpyxl.load_workbook(fileName)  # 获取工作簿的对象
            st: Worksheet = wk[stname]  # 获取当前活跃的工作表
            print(st, type(st))
            ss = st.iter_rows(min_row=minRow, max_row=maxRow, min_col=minCol, max_col=maxCol, values_only=True)
            print(ss)
        except Exception as e:
            print(f"读取Excel文件{fname}失败，原因：{e}")
            # myLogger.exception(f"读取Excel文件{fname}失败，原因：{e}")
        else:
            # myLogger.info(f"成功读取Excel文件{fname}")
            l = [i for i in ss]
            print(f"{l}")
            return l
        finally:
            print(f"处理{fname}文件时未发生异常")
            # myLogger.info(f"处理{fname}文件时未发生异常")
            wk.close()  # 关闭工作簿对象

    # 自定义写入Excel文件
    def doExcelWrite(self,fname,stname,data):
        fileName = os.path.join(myPath, fname)  # 定义文件路径  新文件名
        try:
            myLogger.info(f"开始处理文件{fname}")
            wk = openpyxl.Workbook()  # 创建工作簿对象
            st:Worksheet = wk.create_sheet(stname)  # 创建sheet对象
        except Exception as e:
            print(f"创建Excel文件{fname}失败，原因：{e}")
            myLogger.exception(f"创建Excel文件{fname}失败，原因：{e}")
        else:
            myLogger.info(f"成功创建Excel文件{fname}")
            st.cell(1,1,data)  # 写入数据，写一个单元格
            wk.save(fileName)  # 保存文件
        finally:
            print(f"处理{fname}文件时未发生异常")
            myLogger.info(f"处理{fname}文件时未发生异常")
            wk.close()  # 关闭工作簿对象

# 尝试写入一行多行数据，并保存到Excel文件中,列表循环写入

if __name__ == '__main__':
    df = DoFile()
    # Excel写入一行数据
    # df.doExcelWrite("test1.xlsx","Sheet","hello world")

    # Excel读取指定范围内的单元格数据
    # df.doExcelReadDIV("jiaYunSuan.xlsx","Sheet1",2,15,1,1)
    # df.doExcelReadDIV("jiaYunSuan.xlsx","Sheet1",2,15,2,5)

    # df.doExcelReadDIV("jiaYunSuan.xlsx","Sheet2",2,18,2,5)

    # df.doExcelReadDIV("jiaYunSuan.xlsx","Sheet3",2,19,2,5)

    # df.doExcelReadDIV("jiaYunSuan.xlsx","Sheet4",2,20,2,5)

    # Excel读取数据
    # df.doExcelRead("test.xlsx")




